package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.vo.SetmealDto;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    //修改数据
    @Override
    public R updateSetmeal(SetmealDto setmealDto) {
        //修改套餐数据
        setmealMapper.updateById(setmealDto);
        //获取套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //删除套餐菜品
        setmealDishMapper.deleteBySetmealId(setmealDto.getId());
        //新增套餐菜品
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改成功");
    }

    //根据分类id获取对应套餐
    @Override
    public R getSetmealsByCategoryId(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealMapper.selectList(qw);
        return R.success(setmeals);
    }

    //启用停用套餐
    @Override
    public R updateSetmealStatus(Integer status, List<Long> ids) {
/*        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            setmealMapper.updateById(setmeal);
        }*/
        for (Long id : ids) {
            setmealMapper.updateStatusById(status,id);
        }
        return R.success("修改成功");
    }
    //数据回显
    @Override
    public R selectSetmealById(Long id) {
        //获取套餐数据
        Setmeal setmeal = setmealMapper.selectById(id);
        //获取菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        //获取套餐菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //封装后返回
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(setmealDishes);
        BeanUtil.copyProperties(setmeal,setmealDto);
        return R.success(setmealDto);
    }




    //删除套餐
    @Override
    public R deleteSetmeal(List<Long> ids) {
        //判断当前是否包含正在售卖的
        Integer count = setmealMapper.selectCountSetmeal(ids);
        if (count > 0){
            return R.error("包含正在售卖的套餐,无法删除");
        }
        //获取具体id
        for (Long id : ids) {
            //根据id删除套餐
            setmealMapper.deleteBySetmealId(id);
            //根据套餐id删除套餐菜品
            setmealDishMapper.deleteBySetmealId(id);
        }
        return null;
    }



    //新增套餐
    @Override
    public R saveSetmeal(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");
    }



    //套餐分页
    @Override
    public R pageSetmeal(Integer page, Integer pageSize, String name) {
        if (!StrUtil.hasEmpty(name)){
            name = "%" + name + "%";
        }
        //使用mybatis
        //获取开始值
         Integer pageNo = (page -1) * pageSize;
        //获取具体值
        List<Setmeal> records = setmealMapper.pageSetmeal(pageNo, pageSize, name);
        //获取总数
        Integer total = setmealMapper.getTotal(name);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return R.success(map);
    }


}
